package com.example.work12.fragment;


import android.app.Application;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.work12.MainActivity;
import com.example.work12.R;
import com.example.work12.adapter.MusicAdapter;
import com.example.work12.application.MyApp;
import com.example.work12.bean.Music;
import com.example.work12.utils.MusicUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MusicFragment extends Fragment {
    private ListView mLv;
    private MainActivity activity;
    private MyApp application;
//    private List<Music> musicList = new ArrayList<>();
    public MusicFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        activity = (MainActivity) getActivity();
        application = (MyApp) activity.getApplication();
        View view = inflater.inflate(R.layout.fragment_music, container, false);


        mLv = (ListView) view.findViewById(R.id.lv);
        List<Music> music = MusicUtils.getMusic(getActivity());
        MusicAdapter musicAdapter = new MusicAdapter(music, getActivity());
        mLv.setAdapter(musicAdapter);

        mLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                application.index = position;
                activity.service1.callPlay(application.index);
            }
        });
        return view;
    }

}
